package sandbox.psyomn;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;


import Model.ComplexTask;

/**
 * Gateway for the SingleTask component.
 * 
 * NOTE do not auto reformat code!!
 * 
 * NOTE we assume primary key is task name
 * 
 * 
 * Also, sqlite3 is stupid and doesn't have booleans, 
 * so if you're wondering that is why I'm using the ints
 * with the really ugly conditionals. :D! 
 * 
 * @author psyomn
 */

public class ComplexTaskGateway implements CRUD<ComplexTask>{

	/**
	 * Set the table name to the particular model we are working on
	 */
	private String mTableName = "ComplexTask";
	
	/** 
	 * The table specification for this object
	 */
	
	/*
	private String mTableSchema = new String(
			"CREATE TABLE IF NOT EXISTS " + mTableName + " ( " + 
			" taskname VARCHAR(50) PRIMARY KEY " +
			" ,taskdate DATETIME " + 
			" ,taskdone INTEGER " +  
			" ,taskfreq VARCHAR(50) " +
			" ,childof VARCHAR(50) " +
			" ,iscomplex INTEGER " + 
			" ); "
			);
	*/
	
	private String mTableSchema = new String(
			"CREATE TABLE IF NOT EXISTS " + mTableName + " ( " +
			"HAHAHAHAHAHA BLOB " + 
					" ); "
			);
	
	
	/**
	 * The statement string for insertions.
	 * Please note that this needs to be prepared 
	 */
	private String mInsertStatementString = new String(
			"INSERT INTO " + mTableName +
			" (taskname,taskdate,taskdone,taskfreq,childof,iscomplex) " + 
			" values (?,?,?,?,?,?); "
			);
	
	/**
	 * The update statement string for updates
	 * Please note that this needs to be prepared.
	 */
	private String mUpdateStatementString = new String(
			"UPDATE " + mTableName + 
			" SET taskdate=?, taskdone=?, taskfreq=?, childof=?, iscomplex=? " + 
			" WHERE taskname=?"
			);
	
	/**
	 * The statement string for deletions
	 * Please note that needs to be prepared.
	 * 
	 * Notice the childof, that makes sure that it
	 * removes nested crap as well.
	 */
	private String mDeleteStatementString = new String(
			" DELETE FROM " + mTableName +  
			" WHERE taskname=? OR childof=?;"
			);
	
	
	public ComplexTaskGateway()
	{
		
	}
	
	@Override
  public ArrayList<ComplexTask> read()
  {
	  // XXX Auto-generated method stub
	  return null;
  }

	@Override
  public void update(ComplexTask t)
  {
	  // XXX Auto-generated method stub
	  
  }

	@Override
  public void create(ComplexTask t)
  {
		try
		{
			Connection conn = Executor.getInst().getConnection(); 
			PreparedStatement state = conn.prepareStatement(mInsertStatementString);
			
			System.out.println(mInsertStatementString);
			
			/* The meaty part. You're preparing, and then executing the batch. */
			
			
			state.setString(1, t.getName());
			state.setDate(2, new java.sql.Date( t.getDueDate().getTime() ));
			state.setInt(3, t.getDone() ? 1 : 0);
			//state.setString(4, t.getRepeat());
			state.addBatch();
			
			
			
			state.executeBatch();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
  }

	@Override
  public void delete(ComplexTask t)
  {
	  // XXX Auto-generated method stub
	  
  }


}
